//
//  ICChatDetailViewController.m
//  ICome
//
//  Created by ENN on 16/3/29.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChatDetailViewController.h"
#import "ICCommonGroup.h"
#import "ICGroupUser.h"
#import "ICSettingArrowItem.h"
#import "ICSettingSwitchItem.h"
#import "ICChatDetailCell.h"
#import "ICSettingNoneItem.h"
#import "ICDetailHeadView.h"
#import "ICOrganizationViewController.h"
#import "ICMessageDatabase.h"
#import "ICComEmpListViewController.h"
#import "ICChatPhotoViewController.h"
#import "ICGroupNameController.h"
#import "ICMessageSearchController.h"
#import "ICGroupUserController.h"
#import "ICChangeMasterController.h"
#import "ICNickNameController.h"
#import "ICGroupFileController.h"
#import "YYPhotoGroupView.h"
#import "ICHeaderFile.h"
#import "iComeApiManager.h"
@interface ICChatDetailViewController ()<UITableViewDataSource,UITableViewDelegate, ICOrganizationCtrDelegate,ICDetailHeadDelegate,UIActionSheetDelegate,ICDeleteGroupUserDelegate,ChangeMasterDelegate,ICGroupNameDelegate,NickNameDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *users;

@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, assign) BOOL isMaster;

@property (nonatomic, copy) NSString *master;

@property (nonatomic, strong) ICDetailHeadView *headView;

@end

@implementation ICChatDetailViewController

-(instancetype)init{
    if(self =[super init])
    {
        self.power = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadData];
    [self setupNav];
    
    [self setupUI];
    
    [self synchronizeGroupInfo];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.group.gId && self.headView) {
        NSString *photoId = [ICMessageDatabase getGroupPhotoIdWithGroupId:self.group.gId];
        [self.headView modifyHeadImageWithPhotoId:photoId withGroup:self.group];
    }
}

#pragma mark - Nav

- (void)setupNav
{
    self.title  = @"详情";
}

- (void)loadData
{
    WEAKSELF
    [ICMessageDatabase getUsersWithGroupId:self.group.gId finish:^(NSArray *array, NSError *error) {
        
        weakSelf.users   = (NSMutableArray *)array;
    }];
}

- (void)setupUI
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    if ([self.group.gType isEqualToString:@"2"] && self.power) { // 2个人以上才显示  self.users.count > 2
        _footerView = nil;
        self.tableView.tableFooterView = self.footerView;
    }

    
    BOOL isMaster              = [ICMessageDatabase isMasterWithGroupId:self.group.gId];
    CGFloat inset              = 14;
    NSArray *subArr            = [NSArray array];
    CGFloat frontCount         = 9;
    if (isMaster) {
        frontCount             = 8;
    }
    if(!self.power){
        frontCount             = 10;
    }
    if (self.users.count > frontCount) {
        subArr                 = [self.users subarrayWithRange:NSMakeRange(0, frontCount)];
    } else {
        if ([self.group.gType isEqualToString:@"1"]) {
            for (ICUser *user in self.users) {
                if (![user.eId isEqualToString:[ICUser currentUser].eId]) {
                    subArr = @[user];
                    self.users = (NSMutableArray *)subArr;
                    break;
                }
            }
        } else {
            subArr                 = self.users;
         }
    }
    NSUInteger count = subArr.count;
    CGFloat btnW     = (App_Frame_Width - 2*inset)/5;
    CGFloat btnH     = btnW+5;
    if (iPhone5) {
        btnH = btnH+20;
    }
    if (iPhone6) {
        btnH = btnH+8;
    }
    if ([ICTools isIPhoneX]) {
        btnH = btnH+8;
    }
    CGFloat headViewH          = 20 + (count / 5)*btnH + btnH;
    if (isMaster) {
        headViewH              = 20 + ((count+1) / 5)*btnH + btnH;
    }
    if(!self.power){
        headViewH              = 20 + (count / 5)*btnH + btnH;
    }
    
    //table header view
    self.group.gName = [ICMessageDatabase getGroupName:self.group.gId];
    self.headView = [[ICDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, headViewH)];
    self.headView.isMaster          = isMaster;
    self.headView.isPower           = self.power;
    self.headView.users             = subArr;
    self.headView.headDelegate      = self;
    self.headView.group             = self.group;
    self.headView.backgroundColor   = [UIColor whiteColor];
    self.tableView.tableHeaderView = self.headView;
    
    if (![self.group.gType isEqualToString:@"2"] && ![self.group.gType isEqualToString:@"11"]) {
        return;
    }
    //table contentInset view
    CGFloat topImageH = ICTopImageH;
    self.tableView.contentInset = UIEdgeInsetsMake(topImageH, 0, 0, 0);
    self.headView.topView.frame = CGRectMake(0, -topImageH, self.tableView.width, topImageH);
    [self.tableView addSubview:self.headView.topView];
    
    [self.tableView scrollToTop];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //往下拉图片放大
    CGFloat y = scrollView.contentOffset.y;
    //向上偏移量变正  向下偏移量变负
    if (y < -ICTopImageH) {
        CGRect frame = self.headView.topView.frame;
        frame.origin.y = y;
        frame.size.height = -y;
        self.headView.topView.frame = frame;
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ICCommonGroup * group = self.dataArray[section];
    return group.items.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICChatDetailCell *cell = [ICChatDetailCell cellWithTableView:tableView];
    ICCommonGroup *group = self.dataArray[indexPath.section];
    ICCommonItem *item = group.items[indexPath.row];
    cell.item = item;
    if(indexPath.row == 0){
        //  if (![group.gType isEqualToString:@"2"] && ![group.gType isEqualToString:@"11"]) {
        if(indexPath.section == 0 && ([self.group.gType isEqualToString:@"2"] ||[self.group.gType isEqualToString:@"11"])){
            [ICTools drawLineWithSuperView:cell.contentView
                             Color:LINEBACKGROUNDCOLOR
                             Frame:CGRectMake(15,0,App_Frame_Width-30,0.5)];
        }else{
            [ICTools drawLineWithSuperView:cell.contentView
                                     Color:LINEBACKGROUNDCOLOR
                                     Frame:CGRectMake(0,0,App_Frame_Width,0.5)];

        }
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0;
}

// 设置section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICCommonGroup *group = self.dataArray[indexPath.section];
    ICCommonItem *item = group.items[indexPath.row];
    // 执行操作
    if (item.option) {
        item.option();
        return;
    }
    if ([item isKindOfClass:[ICSettingArrowItem class]]) {
        ICSettingArrowItem *arrowItem = (ICSettingArrowItem *)item;
        if ([arrowItem.destVcClass isSubclassOfClass:[ICGroupUserController class]]) {
            // 查看成员
            ICGroupUserController *vc = [[ICGroupUserController alloc] init];
            vc.users = self.users;
            vc.gId   = self.group.gId;
            vc.master = self.master;
            [self.navigationController pushViewController:vc animated:YES];
        } else if ([arrowItem.destVcClass isSubclassOfClass:[ICChangeMasterController class]]) {
            ICChangeMasterController * cgMaer = [[ICChangeMasterController alloc] init];
            cgMaer.gId         = _group.gId;
            cgMaer.users       = self.users;
            cgMaer.delegate    = self;
            [self.navigationController pushViewController:cgMaer animated:YES];
        } else if ([arrowItem.destVcClass isSubclassOfClass:[ICMessageSearchController class]]) {
            ICMessageSearchController * searchVC = [[ICMessageSearchController alloc] init];
            searchVC.group                       = _group;
            [self.navigationController pushViewController:searchVC animated:YES];
        } else if ([arrowItem.destVcClass isSubclassOfClass:[ICNickNameController class]]) {
            ICNickNameController *nickVC = [[ICNickNameController alloc] init];
            nickVC.delegate = self;
            nickVC.group = _group;
            [self.navigationController pushViewController:nickVC animated:YES];
        } else if ([arrowItem.destVcClass isSubclassOfClass:[ICGroupFileController class]]) {
            ICGroupFileController *groupFileVC = [[ICGroupFileController alloc] init];
            groupFileVC.gId  = self.group.gId;
            [self.navigationController pushViewController:groupFileVC animated:YES];
        }
    }
}

#pragma mark - Private
// 重新刷新数据和UI
- (void)refreshData
{
    self.users = nil;
    [self loadData];
    [self setupUI];
    [self.tableView reloadData];
}

#pragma mark - ICOrganizationCtrDelegate

- (void)appendUserWithDictionary:(NSDictionary *)dict {
    [MBProgressHUD showLoadingActivityView:self.view];
    [[ICSocketRequestManager sharedInstance] appendUserWithObject:dict Success:^(id response) {
        NSDictionary *data = [response objectForKey:@"data"];
        ICGroup *group = [ICGroup mj_objectWithKeyValues:data];
        if ([group.gType isEqualToString:@"1"]) {
              // 双人组新创建了组
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationDidCreatedConversation object:nil];
            for (ICUser *user in group.userList) {
                if (![user.eId isEqualToString:[ICUser currentUser].eId]) {
                    group.gName  = user.eName;
                    break;
                }
            }
        }
        group.valid = 1; // 默认肯定可用
        group.busy = 0;  // 默认是0，不免打扰
//        group.lastMsgTime = [ICMessageHelper currentMessageTime];
        if ([[data objectForKey:@"master"] isEqualToString:[ICUser currentUser].eId]) {
            group.master = 1;
        } else {
            group.master = 0;
        }
        [ICMessageDatabase saveGroup:group];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showSuccess:@"人员添加成功"];
        
        // 添加了群成员需要刷新界面
        self.dataArray = nil;
        [self refreshData];
        
    } Failure:^(NSError *error, id other) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:error.domain];
    }];
}

#pragma mark - ICDeleteGroupUserDelegate

- (void)deleteSuccess
{
    self.dataArray = nil;
    [self refreshData];
}

#pragma mark - ChangeMasterDelegate

- (void)changeMaster
{
    self.isMaster = NO;
    self.users = nil;
    [self loadData];
    [self setupUI];
    _dataArray = nil;
    [self synchronizeGroupInfo];
    [self.tableView reloadData];
}

#pragma mark - ICChangeGroupNameDelegate

- (void)changeGroupNamed
{
    self.dataArray = nil;
    [self refreshData];
}


#pragma makr - ICDetailHeadDelegate

- (void)deleteBtnClicked {
    ICComEmpListViewController *VC = [[ICComEmpListViewController alloc] init];
    VC.users    = self.users;
    VC.gId      = self.group.gId;
    VC.delegate = self;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)addBtnClicked
{
    WEAKSELF
    if ([[iComeApiManager instance].delegate respondsToSelector:@selector(appendGroupUsersFromViewC::)]) {
        
        [[iComeApiManager instance].delegate appendGroupUsersFromViewC:self :^(id result) {
            NSString *gType = [ICMessageDatabase groupTypeWithGroupId:weakSelf.group.gId];
            if ([gType isEqualToString:@"1"]) {  // 双人组
                NSMutableArray *list = [[NSMutableArray alloc] init];
                for (NSDictionary *dict in result) {
                    ICEmpInfoModel *model = [[ICEmpInfoModel alloc] init];
                    model.eId = dict[@"eId"];
                    model.eName = dict[@"eName"];
                    model.select = YES;
                    [list addObject:model];
                }
                NSDictionary *param = [ICCreateGroupManager createMultGroupParamWithUserList:list];
                [ICCreateGroupManager createMultGroupWith:param Handle:^(ICGroup *group) {
                    ICChatViewController *vc = [[ICChatViewController alloc] init];
                    vc.group = group;
                    vc.backFlag = @"3";
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
            } else {
                NSMutableArray *userList = [[NSMutableArray alloc] init];
                for (NSDictionary *dict in result) {
                    [userList addObject:dict[@"eId"]];
                }
                NSDictionary *param = @{@"gId":weakSelf.group.gId,
                                        @"eIds":userList};
                [weakSelf appendUserWithDictionary:param];
            }
        }];
    } else {
        ICOrganizationViewController *vc = [[ICOrganizationViewController alloc] init];
        vc.delegate = self;
        vc.CtrStyle = CtrStyleEmploye_sdk;
        vc.titleName = @"添加联系人";
        vc.isAppendUser = YES;
        vc.kkGroupType = GroupStyleCreateEmployee;
        vc.gId = self.group.gId;
        vc.eId = [ICUser currentUser].eId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)headBtnClicked:(NSInteger)index User:(ICUser *)user
{
    NSDictionary *param = @{@"eId":user.eId};
    if ([user.eId rangeOfString:@"bot"].location == NSNotFound) {
        if ([[iComeApiManager instance].delegate respondsToSelector:@selector(openContactsDetailsFromViewC::)]) {
            [[iComeApiManager instance].delegate openContactsDetailsFromViewC:self:param];
        } else {
            UIViewController * vc = [[IComMediator sharedInstance] IComMediator_ContactsDetailViewController:param];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else
    {
        UIViewController *vc = [[IComMediator sharedInstance] IComMediator_RobotDetailViewController:param];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)changeGroupName
{
    ICGroupNameController *groupNameVC = [[ICGroupNameController alloc] init];
    groupNameVC.delegate                 = self;
    groupNameVC.group                    = self.group;
    [self.navigationController pushViewController:groupNameVC animated:YES];
}

- (void)changeGroupHeadImg
{
    // 添加群主控制 （非群主也进入界面）
    if(self.isMaster){
    ICChatPhotoViewController *vc = [ICChatPhotoViewController editorHeadImageGroupHeadimage:YES Group:self.group isMaster:self.isMaster];
    [self.navigationController pushViewController:vc animated:YES];
    }else{

        UIView *fromView = nil;
        NSMutableArray *items = [[NSMutableArray alloc] init];
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = self.headView.topHeadBtn.imageView;
        //image;
        NSString *URL = [ICTools getOglImageURL:(self.group.photoId)];
        item.largeImageURL = [NSURL URLWithString:URL];
        [items addObject:item];
        fromView = self.headView.topHeadBtn.imageView;
        YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
        [v presentFromImageView:fromView toContainer:self.navigationController.view fromViewC:self animated:YES completion:nil];

    }
}

#pragma mark - nickNameDelegate

- (void)changedNickName
{
    self.dataArray = nil;
    [self refreshData];
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (actionSheet.tag) {
        case 100:
        {
            if (buttonIndex == 0) {
                [ICMessageDatabase clearChatRecord:self.group.gId];
                if ([self.delegate respondsToSelector:@selector(recordDidCleared)]) {
                    [self.delegate recordDidCleared];
                }
            }
        }
            break;
            
        case 101:
        {
            if (buttonIndex == 0) {
                [MBProgressHUD showMessage:@"正在加载" toView:self.view];
                NSDictionary *param = @{@"gId":self.group.gId};
                [[ICSocketRequestManager sharedInstance] disbandGroupWithObject:param Success:^(id response) {
                    [MBProgressHUD hideHUDForView:self.view];
                    
                    [MBProgressHUD showSuccess:@"解散成功"];
                    [ICMessageDatabase invalidGroup:self.group.gId valid:NO];
                    [self.navigationController popViewControllerAnimated:YES];
                    
                } Failure:^(NSError *error, id other) {
                    [MBProgressHUD hideHUDForView:self.view];
                    [MBProgressHUD showSuccess:@"解散失败,请重试"];
                }];
            }
        }
            break;
            
        case 102:
        {
            if (buttonIndex == 0) {
                [MBProgressHUD showMessage:@"正在加载" toView:self.view];
                NSDictionary *param = @{@"gId":self.group.gId,
                                        @"eId":[ICUser currentUser].eId};
                [[ICSocketRequestManager sharedInstance] existGroupWithObject:param Success:^(id response) {
                    
                    [MBProgressHUD hideHUDForView:self.view];
                    [MBProgressHUD showError:@"退群成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                    [ICMessageDatabase invalidGroup:self.group.gId valid:NO];
                    
                } Failure:^(NSError *error, id other) {
                    [MBProgressHUD hideHUDForView:self.view];
                    [MBProgressHUD showError:@"退群失败,请重试"];
                }];
            }
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark - event response

- (void)outBtnClick {
    // 1:群主/2:非群主 操作为解散和退出
    if ([ICMessageDatabase isMasterWithGroupId:self.group.gId]) {
        
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"退出后不会通知群聊中其他成员，且不会再接收此群聊消息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil];
        actionSheet.tag = 101;
        [actionSheet showInView:self.view];
        
    } else {
        
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"退出后不会通知群聊中其他成员，且不会再接收此群聊消息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil];
        actionSheet.tag = 102;
        [actionSheet showInView:self.view];
    }
}


// 同步群信息
- (void)synchronizeGroupInfo
{
    NSDictionary *param = @{
                            @"gId":self.group.gId,
                            @"eId":[ICUser currentUser].eId,
                            @"lastUpdate":@0
                            };
    WEAKSELF
    [[ICSocketRequestManager sharedInstance] groupInfoWithObject:param Success:^(id response) {
        NSDictionary *data = [response objectForKey:@"data"];
        NSDictionary *gInfo = [data objectForKey:@"gInfo"];
        ICGroup *group = [ICGroup mj_objectWithKeyValues:gInfo];
        if ([[gInfo objectForKey:@"master"] isEqualToString:[ICUser currentUser].eId]) {
            group.master = 1;
            weakSelf.isMaster = 1;
        } else {
            group.master = 0;
            weakSelf.isMaster = 0;
        }
        self.master = [gInfo objectForKey:@"master"];
        [ICMessageDatabase updateGroupInfo:group];
        weakSelf.group = group;
        NSArray *arrs    = [data objectForKey:@"gUsers"];
        NSMutableArray *groupUsers = [[NSMutableArray alloc] init];
        NSMutableArray *saveUsers = [[NSMutableArray alloc] init];

        for (NSDictionary *d in arrs) {
            ICGroupUser * groupUser = [[ICGroupUser alloc] init];
            ICUser * gUser   = [ICUser mj_objectWithKeyValues:d];
            if (gUser.valid == 0) {
                [ICMessageDatabase deleteGroupUserWithGroupId:group.gId eId:gUser.eId];
            } else {
                groupUser.eId = gUser.eId;
                groupUser.nName = gUser.nName == nil ? gUser.eName : gUser.nName;
                groupUser.valid = gUser.valid;
                [saveUsers addObject:gUser];
                [groupUsers addObject:groupUser];
//                [ICMessageDatabase saveUser:gUser];
//                [ICMessageDatabase saveGroupUserWithGroupId:group.gId groupUser:groupUser];
            }
        }
        [ICMessageDatabase saveUsers:saveUsers];
        [ICMessageDatabase saveGroupUsersWithGroupId:group.gId groupUsers:groupUsers];
        _dataArray = nil;
        _footerView = nil;
        [weakSelf loadData];
        [weakSelf setupUI];
        [weakSelf.view layoutIfNeeded];
        [weakSelf.tableView reloadData];
        
        [ICMessageHelper setGroupInfoLastUpdate:[data objectForKey:@"lastUpdate"] gId:self.group.gId];
    } Failure:^(NSError *error, id other) {
        [MBProgressHUD hideHUDForView:self.view];
        NSLog(@"ICGroupInfoAPI request error");
    }];
}

#pragma mark - Getter

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        __weak typeof(self) weakSelf = self;
        ICCommonItem * discover    = [ICSettingSwitchItem itemWithIcon:nil title:@"置顶聊天" destVcClass:nil parameter:_group];
        ICCommonItem *companyDoc   = [ICSettingSwitchItem itemWithIcon:nil title:@"消息免打扰" destVcClass:nil parameter:_group.gId];
        NSString *detailTitle = [ICMessageDatabase nickNameWithEid:[ICUser currentUser].eId gId:_group.gId];
        if (detailTitle == nil) {
            detailTitle = [ICMessageDatabase userNameWitheId:[ICUser currentUser].eId];
        }
        ICCommonItem * nickItem   = [ICSettingArrowItem itemWithIcon:nil title:@"昵称" detailTitle:detailTitle destVcClass:[ICNickNameController class] parameter:nil];
        ICCommonGroup * mesGroup   = [[ICCommonGroup alloc] init];
        if ([_group.gType isEqualToString:@"0"]||[_group.gType isEqualToString:@"1"]) {
            mesGroup.items = @[discover,companyDoc];
        } else {
            mesGroup.items = @[discover,companyDoc,nickItem];
        }
        
        ICCommonItem * fileItem    = [ICSettingArrowItem itemWithIcon:nil title:@"会话文件" destVcClass:[ICGroupFileController class] parameter:nil];
        ICCommonItem *companyNews  = [ICSettingArrowItem itemWithIcon:nil title:@"查找会话记录" destVcClass:[ICMessageSearchController class] parameter:nil];
        ICCommonItem *noneItem     = [ICSettingNoneItem itemWithIcon:nil title:@"清空会话记录" destVcClass:nil parameter:nil];
        noneItem.option            = ^ {
            UIActionSheet *sheet   = [[UIActionSheet alloc] initWithTitle:nil delegate:weakSelf cancelButtonTitle:@"取消" destructiveButtonTitle:@"清空会话记录" otherButtonTitles:nil, nil];
            sheet.tag = 100;
            [sheet showInView:weakSelf.view];
        };
        ICCommonItem *masterCg   = [ICSettingArrowItem itemWithIcon:nil title:@"移交会话管理权" destVcClass:[ICChangeMasterController class] parameter:nil];
        ICCommonGroup * comGroup   = [[ICCommonGroup alloc] init];
        if ([self.group.gType isEqualToString:@"2"] && self.isMaster) {
            comGroup.items             = @[masterCg,fileItem,companyNews,noneItem];
        } else {
            comGroup.items             = @[fileItem,companyNews,noneItem];
        }
        
        if(!self.power)
        {
            comGroup.items             = @[fileItem,companyNews,noneItem];
        }
        
        if ([self.group.gType isEqualToString:@"2"] || [self.group.gType isEqualToString:@"11"] ) { // 群
            ICCommonItem * usersItem   = [ICSettingArrowItem itemWithIcon:nil title:@"显示成员" detailTitle:[NSString stringWithFormat:@"%ld人",_users.count] destVcClass:[ICGroupUserController class] parameter:nil];
            ICCommonGroup * usersGroup = [[ICCommonGroup alloc] init];
            usersGroup.items = @[usersItem];
            [_dataArray addObject:usersGroup];
            [_dataArray addObject:mesGroup];
            [_dataArray addObject:comGroup];
        } else {
            [_dataArray addObject:mesGroup];
            [_dataArray addObject:comGroup];
        }
    }
    return _dataArray;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.userInteractionEnabled = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = BACKGROUNDCOLOR;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)footerView {
    if (_footerView == nil) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, APP_Frame_Height - 44 - 80, APP_Frame_Height, 80)];
        UIButton *logoutBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, 46)];
        logoutBtn.backgroundColor = [UIColor whiteColor];
        [logoutBtn setTitleColor:ICRGB(0xfc786e) forState:UIControlStateNormal];
        logoutBtn.titleLabel.font = ICBOLDFont(17);
        logoutBtn.layer.borderWidth = 0.5;
        logoutBtn.layer.borderColor = ICRGB(0xe5e6e7).CGColor;
        [logoutBtn addTarget:self action:@selector(outBtnClick) forControlEvents:UIControlEventTouchUpInside];
        // 1:群主/2:非群主 操作为解散和退出
        if (self.isMaster) {
            [logoutBtn setTitle:@"解散当前会话" forState:UIControlStateNormal];
        } else {
            [logoutBtn setTitle:@"退出当前会话" forState:UIControlStateNormal];
        }
        [_footerView addSubview:logoutBtn];
    }
    return _footerView;
}

- (void)dealloc
{
    //    NSLog(@"ICChatDetailViewController--dealloc");
}


@end
